package thread;

public class Demo17 {
    private static int count = 0;

    private static Object locker = new Object();
    private static Object locker2 = new Object();

//    private static String s = "hello";
//    private static List<String> list = new ArrayList<>();
//
//    private static int a = 10;

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            //for循环仍然是并发，只有锁里面是串行的
            //只是把一小部分串行了，大部分还是并发
            for (int i = 0; i < 50000; i++) {
                //通过锁对象对进行后续的锁定
                //{}里面的代码，就是要打包到一起的代码
                //类对象也是一种对象
                synchronized (Demo17.class) {
                    count++;
                }
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                //两个线程必须都锁
                synchronized (Demo17.class) {
                    count++;
                }
            }
        });

        t1.start();
        t2.start();

        t1.join();
        //t2.start();
        t2.join();

        System.out.println("count=" + count);
    }
}
